package com.wannengbang.cloudleader.homepage.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.RankingListBean;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 订单列表
 */
public class RankingListAdapter extends RecyclerView.Adapter<RankingListAdapter.ViewHolder> {
    private List<RankingListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private String status;


    public RankingListAdapter(List<RankingListBean.DataBean.ItemListBean> listsBeans, String status) {
        this.listsBeans = listsBeans;
        this.status = status;
    }

    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_ranking_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RankingListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvName.setText(dataBean.getName());
        if (position == 0) {
            holder.tvRanking.setText("");
            holder.tvRanking.setBackgroundResource(R.mipmap.ic_ranking_jinpai);
        } else if (position == 1) {
            holder.tvRanking.setText("");
            holder.tvRanking.setBackgroundResource(R.mipmap.ic_ranking_yinpai);
        } else if (position == 2) {
            holder.tvRanking.setText("");
            holder.tvRanking.setBackgroundResource(R.mipmap.ic_ranking_tongpai);
        } else {
            holder.tvRanking.setText(String.valueOf(position + 1));
            holder.tvRanking.setBackgroundColor(Color.TRANSPARENT);
        }
        if ("1".equals(status) || "3".equals(status)) {
            if (!TextUtils.isEmpty(dataBean.getMoney())) {
                holder.tvMoney.setText(dataBean.getMoney() + "台");
            }else {
                holder.tvMoney.setText(dataBean.getNumber() + "台");
            }
        } else {
            String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getMoney()) / 100);
            holder.tvMoney.setText(money + "元");
        }
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_ranking)
        TextView tvRanking;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_money)
        TextView tvMoney;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
